package com.api.oa.controller;


import com.api.oa.model.entity.OaMessage;
import com.api.oa.services.IOaMessageService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oa/org/message")
public class OaMessageController extends BaseController {


    @Autowired
    private IOaMessageService oaMessageService;


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody OaMessage org) {
        try {
            return success(oaMessageService.add(org));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable Long id) {
        try {
            oaMessageService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody OaMessage org) {
        try {
            return oaMessageService.update(org) == 1 ? success("修改成功!") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam String from, @RequestParam String to, @RequestParam(defaultValue = "0") Integer pageIndex, @RequestParam Integer pageSize) {
        try {

            return success(oaMessageService.list(from, to, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null : oaMessageService.one(id));
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }

    @RequestMapping(value = "/video", method = RequestMethod.GET)
    public MyResult videoTo(@RequestParam(defaultValue = "") String from, @RequestParam(defaultValue = "") String to) {
        try {
            return success(oaMessageService.sendVideo(from, to));
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }


}
